<template>
  <el-dialog
    :title="option.title"
    :visible.sync="option.isShow"
    width="70%"
    :close-on-click-modal="false"
    @open="openDialog"
    @close="closeDialog('ruleForm')"
  >
    <el-row :gutter="10">
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-col :span="12">
          <el-form-item label="交易所属渠道" prop="TRANSCHANNEL">
            <el-select v-model="ruleForm.TRANSCHANNEL" :disabled="this.option.flag =='update'?true:false" filterable clearable placeholder="请选择交易所属渠道" size="small">
              <el-option
                v-for="item in channelOption"
                :key="item.FIELDKEY"
                :label="item.FIELDVALUE"
                :value="item.FIELDKEY"
              ></el-option>
            </el-select>
            </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="交易码" prop="TRANSCODE">
            <el-input size="small" placeholder="请输入交易码" v-model="ruleForm.TRANSCODE" :disabled="this.option.flag =='update'?true:false"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="交易名称" prop="TRANSNAME">
            <el-input size="small" placeholder="请输入交易名称" v-model="ruleForm.TRANSNAME"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="交易类型" prop="TRANSTYPE">
              <el-select v-model="ruleForm.TRANSTYPE" filterable clearable placeholder="请选择交易类型" size="small">
                <el-option
                  v-for="item in transTypeOption"
                  :key="item.code"
                  :label="item.val"
                  :value="item.code"
                ></el-option>
              </el-select>
            </el-form-item>
        </el-col>

        <el-col :span="24">
          <el-form-item>
            <el-button type="primary" size="mini" @click="submitForm('ruleForm')">保存</el-button>
            <el-button @click="resetForm('ruleForm')" size="mini">重置</el-button>
          </el-form-item>
        </el-col>
      </el-form>
    </el-row>
  </el-dialog>
</template>

<script>
import getGlobalParams from "@/common/utils/getGlobalParams";
import request from "@/common/utils/requestWFM";
import tradeMainTain from "@/message/wfm/FTEForecast/tradingManage/tradeMainTain";

export default {
  props: {
    option: Object
  },
  computed: {
    isShow: {
      get: function() {
        return this.option.isShow;
      },
      set: function(newValue) {
        this.option.isShow = newValue;
      }
    }
  },
  mounted() {},
  data() {
    return {
      transTypeOption: [],
      channelOption:'',
      ruleForm: {
        TRANSCHANNEL: "",
        TRANSCODE: "",
        TRANSNAME: "",
        TRANSTYPE: ""
      },
      rules: {
        // CHANNELTYPE: [{
        //   required: true,
        //   message: '请选择渠道',
        //   trigger: 'change'
        // }],
        // SCORETEMPLATEID: [{
        //   required: true,
        //   message: '请输入模板编号',
        //   trigger: 'change'
        // }]
      },
      loading: false
    };
  },
  created() {},
  methods: {
    openDialog() {
      if (this.$refs["ruleForm"] != undefined) {
        this.$refs["ruleForm"].clearValidate();
      }
      this.channelOption = this.option.channelOption;
      this.transTypeOption = this.option.transTypeOption;
      if (this.option.flag == "add") {
        this.ruleForm = {
          TRANSCHANNEL: "",
          TRANSCODE: "",
          TRANSNAME: "",
          TRANSTYPE: ""
        };
      } else {
        this.ruleForm = this.option.row;
      }
    },
    closeDialog(formName) {
      if (this.$refs[formName] != undefined) {
        this.$refs[formName].clearValidate();
      }
      // this.$refs[formName].clearValidate();
      this.$refs[formName].resetFields();
    },
    //保存
    submitForm(formName) {
      this.$refs[formName].validate(async valid => {
        if (valid) {
          let resBody;
          resBody = new tradeMainTain();
          resBody.data.TRANSCHANNEL = this.ruleForm.TRANSCHANNEL;
          resBody.data.TRANSCODE = this.ruleForm.TRANSCODE;
          resBody.data.TRANSNAME = this.ruleForm.TRANSNAME;
          resBody.data.TRANSTYPE = this.ruleForm.TRANSTYPE;
          if(this.option.flag == "add"){
            resBody.data.OPERTYPE = "01";
          }else{
            resBody.data.OPERTYPE = "02";
          }
          
          let response = await request(resBody);
          if (response.SYS_HEAD.ReturnCode == "000000") {
            if (this.option.flag == 'update') {
                this.$message({
                  type: 'success',
                  message: '修改成功！'
                })
              } else {
                this.$message({
                  type: 'success',
                  message: '新增成功！'
                })
              }
            this.isShow = false;
            this.$emit("refreshTable");
          } else {
            this.$message({
              type: "error",
              message:
                "请求失败，错误码:" +
                response.SYS_HEAD.ReturnCode +
                "，错误信息：" +
                response.SYS_HEAD.ReturnMessage
            });
          }
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      if (this.$refs[formName] != undefined) {
        this.$refs[formName].clearValidate();
        this.$refs[formName].resetFields();
      }
    }
  }
};
</script>

<style scoped>
</style>
